package com.space.school.services.impl;

import com.space.school.dao.ProductDao;
import com.space.school.dao.impl.ProductDaoImpl;
import com.space.school.domain.Category;
import com.space.school.domain.Product;
import com.space.school.domain.User;
import com.space.school.services.ProductService;
import com.space.school.utils.PageBean;

import java.util.List;
import java.util.Map;

public class ProductServiceImpl implements ProductService {
    private ProductDao productDao = new ProductDaoImpl();

    @Override
    public PageBean<Product> search(int currentPage, int pageSize, Map<String, String[]> map) {
        int start = (currentPage - 1) * pageSize;
        List<Product> list = productDao.search(start, pageSize, map);
        PageBean<Product> pb = new PageBean<>();
        pb.setList(list);
        //        总数居
        int count = productDao.count(map);
        pb.setTotalCount(count);
//        每页数据数
        pb.setRows(pageSize);
//        当前页
        pb.setCurrentPage(currentPage);
//        总页数
//        int totalPage = (int) Math.ceil((double) list.size() / pageSize);
        int totalPage = (count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public int delete(int id) {
        return productDao.delete(id);
    }


    @Override
    public void update(Map<String, String[]> map) {
        productDao.update(map);
    }

    @Override
    public Product findById(int id) {
        return productDao.findById(id);
    }

    @Override
    public void updateStatus(int id, String status) {
        if (status.equals("1")) {
            status = "0";
        } else {
            status = "1";
        }
        productDao.updateStatus(id, status);
    }

    @Override
    public void save(Product product) {
        productDao.save(product);
    }

    @Override
    public boolean isInsert(String product_name) {
        Product product = productDao.isInsert(product_name);
        if (product == null) {
            return true;
        }
        return false;
    }

    @Override
    public List<Category> findAll() {
        return productDao.findAll();
    }

}


